package com.tvsinfo.thread.waitnotify;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author ruyi.song
 * @Date 2020/03/30 10:40:53
 */
public class WaitNotifyDemo {

    private static AtomicInteger add = new AtomicInteger(0);

    /**
     * 生成者
     */
    private static class Product implements Runnable {
        private List<Integer> contain;


        public Product(List<Integer> contain) {
            this.contain = contain;
        }

        @Override
        public void run() {
            while (true) {
                synchronized (contain) {
                    while (contain.size() == 8) {
                        try {
                            System.out.println("生产者满了");
                            contain.wait();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    //模拟生产
                    try {
                        Thread.sleep(1000L);
                        Integer integer = add.getAndIncrement();
                        contain.add(integer);
                        contain.notifyAll();
                        System.out.println("id:" + Thread.currentThread().getId() + " , name:" + Thread.currentThread().getName() + " 生产" + integer);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
//                    try {
//                        contain.wait();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                }
            }
        }
    }

    /**
     * 消费者
     */
    private static class Comsumer implements Runnable {
        private List<Integer> contain;

        public Comsumer(List<Integer> contain) {
            this.contain = contain;
        }

        @Override
        public void run() {
            while (true) {
                synchronized (contain) {
                    while (contain.isEmpty()) {
                        try {
                            System.out.println("id:" + Thread.currentThread().getId() + " , name:" + Thread.currentThread().getName() + " 消费者空了");
                            contain.wait();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        Thread.sleep(1000L);
                        System.out.println("id:" + Thread.currentThread().getId() + " , name:" + Thread.currentThread().getName() + " 消费" + contain.get(0).intValue());
                        contain.remove(0);
                        contain.notifyAll();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
    }


    public static void main(String[] args) {
        List<Integer> contain = new ArrayList<>();
        new Thread(new Comsumer(contain)).start();
        new Thread(new Comsumer(contain)).start();
        new Thread(new Product(contain)).start();

    }

}
